package searchAlgs;

/**
 *
 * @author Ruan,Tiago
 */
import Graph.Graph;
import Graph.Vertex;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class AlgBFS {
    
    private Map<Integer,String> color = new HashMap();
    private Vertex s,u,f;
    private Graph graph = new Graph();
    private Map<Integer,Integer> d = new HashMap();
    private LinkedList<Vertex> q = new LinkedList();
    private Map<Integer,Vertex> precursor;
   

    public AlgBFS(Graph g,Vertex s, Vertex f){
        graph = g;
        this.s = s;
        this.f = f;
        precursor = new HashMap();
        for(Map.Entry<Integer,Vertex> entry: graph.getVertexes().entrySet()){
            Integer integer = entry.getKey();
            Vertex vertex = entry.getValue();
            color.put(vertex.getId(), "WHITE");
        }
        
    }

    public void doBFS(Graph g){
        d.put(s.getId(),0);
        color.put(s.getId(),"GRAY");
        q = new LinkedList();
        q.add(s);
        u = null;
        while(!q.isEmpty()){
            //System.out.print("FILA = "+ q.toString());
            //System.out.print("PRECURSOR = "+ precursor);
            u = q.remove();
            for(int i = 0;i<u.adj().size();i++){
                if(color.get(u.adj().get(i).getId()).equals("WHITE")){
                    q.add(u.adj().get(i));
                    color.put(u.adj().get(i).getId(),"GRAY");
                    precursor.put(u.adj().get(i).getId(), u);
                    //System.out.println(precursor.get(u.adj().get(i).getId()));
                    if(!d.isEmpty()){
                        d.put(u.adj().get(i).getId(),1);
                    }else{
                        d.put(u.adj().get(i).getId(),d.get(i)+1);
                    }
                }
                    
            }
            color.remove(u.getId());
            color.put(u.getId(),"BLACK");
        }
        Path path = new Path();
        path.printPath(g, s, f, precursor);
    }
    

    public Map<Integer,String> getColor() {
        return color;
    }

    public void setColor(Map<Integer,String> color) {
        this.color = color;
    }

    public Map<Integer,Integer> getD() {
        return d;
    }

    public void setD(Map<Integer,Integer> d) {
        this.d = d;
    }

    public Vertex getS() {
        return s;
    }

    public void setS(Vertex s) {
        this.s = s;
    }

    /**
     * @return the f
     */
    public Vertex getF() {
        return f;
    }

    /**
     * @param f the f to set
     */
    public void setF(Vertex f) {
        this.f = f;
    }
    
    
    

}